package com.shangma.cn.entity.base;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shangma.cn.commons.utils.SpringContainerUtils;
import com.shangma.cn.commons.utils.TokenService;
import com.shangma.cn.commons.valid.AddGroup;
import com.shangma.cn.commons.valid.UpdateGroup;
import lombok.Data;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import java.time.LocalDateTime;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/8  11:50
 * 文件含义:
 */
@Data
public class BaseEntity {


    @TableId(type = IdType.AUTO)
    @Null(groups = {AddGroup.class})
    @NotNull(groups = {UpdateGroup.class})
    private Long id;
    @JsonIgnore
    private LocalDateTime createTime;
    @JsonIgnore
    private LocalDateTime updateTime;
    private Long createBy;
    private Long updateBy;


    public void setData() {
        if (id == null) {
            this.createTime = LocalDateTime.now();
            this.createBy = SpringContainerUtils.getBean(TokenService.class).getLoginAdminId();
        } else {
            this.updateTime = LocalDateTime.now();
            this.updateBy = SpringContainerUtils.getBean(TokenService.class).getLoginAdminId();
        }
    }
}
